package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
// @Transactional
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private StrategyCommentRepository strategyCommentRepository;


    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);
        // 添加攻略评论中的评论时间
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {

        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        // 当实现修改操作时,如果数据库列表中的字段和 domain里面的一致就
        // 不用先查后修改操作.
       /* String id = strategyComment.getId();
        StrategyComment newStrategyComment = mongoTemplate.findById(id, StrategyComment.class);*/
        strategyCommentRepository.save(strategyComment);

    }

    @Override
    public StrategyComment get(String id) {

        return strategyCommentRepository.findById(id).orElse(null);
    }


    @Override
    public Page<StrategyComment> query(StrategyCommentQuery qo) {
        // 创建查询对象
        Query query = new Query();
        // 查询数据总数
        long totalCount = mongoTemplate.count(query, StrategyComment.class);
        //
        if (totalCount == 0){
            // 返回一个空白页面
            return Page.empty();
        }
        // 添加以攻略为限制条件
       query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        // 分页
        Pageable page = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        // 给查询加入限制条件
        query.with(page);
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
        // 实现接口方法是调用分页插件page
        return new PageImpl<>(list,page,totalCount);
    }

    // 点赞
    @Override
    public void commentThumb(String cid,UserInfo userInfo) {
        // 获取用户id
        String uid = userInfo.getId();
        // 通过评论id获取评论对象
        StrategyComment strategyComment = this.get(cid);
        // 获取点赞用户id记录集合
        List<String> uidList = strategyComment.getThumbuplist();
        // 如果点赞用户id记录集合中不存在对应的用户id,表示进行点赞操作
        if (!uidList.contains(uid)){
            // 点赞数加一
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1);
            // 将用户id放进集合中
            uidList.add(uid);
        }else {
            // 点赞数减一
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1);
            // 将用户id从集合中移除
            uidList.remove(uid);
        }
        // 更新
        this.update(strategyComment);
    }

}
